package com.testpro.imp;

import cn.hutool.core.io.resource.ClassPathResource;
import com.testpro.abs.AbctactSVNLogAnalys;
import com.testpro.bean.svnLogDetiles;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author HelloWord testdevops
 * @description 分析实现
 * @create 2019-06-06 12:28
 */
public class SvnlogAnalysimp extends AbctactSVNLogAnalys {

  protected void initConfig() {
    ClassPathResource resource = new ClassPathResource("application.properties");
    Properties properties = new Properties();
    try {

      properties.load(resource.getStream());
      if (!properties.isEmpty()) {
        for (String key : properties.stringPropertyNames()) {
          if (key.equals("filename")) {
            this.setFilename(properties.getProperty(key));
          }
          if (key.equals("startDate")) {
            this.setStartDate(properties.getProperty(key));
          }

          if (key.equals("endDate")) {
            this.setEndDate(properties.getProperty(key));
          }
          if (key.equals("url")) {
            this.setUrl(properties.getProperty(key));
          }
          if (key.equals("username")) {
            this.setUsername(properties.getProperty(key));
          }
          if (key.equals("password")) {
            this.setPassword(properties.getProperty(key));
          }
        }
      }

    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException(e.getMessage());
    }
  }

  protected List<svnLogDetiles> checkLogsAnasy(
      String startDate, String endDate, String url, String username, String password) {

    //    SVNRepositoryFactoryImpl.setup();
    DAVRepositoryFactory.setup();
    long startRevision = 0;
    long endRevision = -1; // HEAD (the latest) revision
    Collection logEntries = null;
    SVNRepository repository = null;
    try {
      repository = DAVRepositoryFactory.create(SVNURL.parseURIEncoded(url));

      ISVNAuthenticationManager authManager =
          SVNWCUtil.createDefaultAuthenticationManager(username, password);
      repository.setAuthenticationManager(authManager);
      logEntries = repository.log(new String[] {""}, null, startRevision, endRevision, true, true);
    } catch (Exception e) {
      e.printStackTrace();
    }
    List<svnLogDetiles> list = getsvnLogDetiles(logEntries);
    return list;
  }

  private List<svnLogDetiles> getsvnLogDetiles(Collection logEntries) {

    List<svnLogDetiles> logDetilesList = new ArrayList<>();
    SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    for (Iterator entries = logEntries.iterator(); entries.hasNext(); ) {
      SVNLogEntry logEntry = (SVNLogEntry) entries.next();

      if (logEntry.getChangedPaths().size() > 0) {
        Set changedPathsSet = logEntry.getChangedPaths().keySet();

        for (Iterator changedPaths = changedPathsSet.iterator(); changedPaths.hasNext(); ) {
          svnLogDetiles logDetiles = new svnLogDetiles();
          final Date date = logEntry.getDate();
          logDetiles.setAuthor(logEntry.getAuthor());
          System.out.println(aDate.format(date));
          logDetiles.setMessage(logEntry.getMessage());
          logDetiles.setDate(aDate.format(date));
          logDetiles.setRevision(logEntry.getRevision() + "");
          SVNLogEntryPath entryPath =
              (SVNLogEntryPath) logEntry.getChangedPaths().get(changedPaths.next());
          logDetiles.setChangedStatus(entryPath.getType() + "");
          logDetiles.setChangedPaths(entryPath.getPath());
          logDetilesList.add(logDetiles);
        }
      }
    }

    return logDetilesList;
  }
}
